<?php

namespace App\Models\CacheModel\chat;

use App\Models\Api\BaseCache;
use think\Log;

class HzChatRecordDetailsZanData extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.hz_chat_record_details_zan_data';

    /**
     * 记录 赞同与反对
     * @param int $uid
     * @param int $chatDetailId
     * @param int $status
     * @param array $feedbackReasonArray
     * @param int $pid
     * @param int $questionType 问题类型 0闲聊｜未知  1=>命中 2=>默认回复
     * @param int $qid 问题ID
     * @return bool
     */
    public function setZan(int $uid, int $chatDetailId, int $status, array $feedbackReasonArray, int $pid, int $questionType, int $qid)
    {
        try
        {
            $this->where(['chat_detail_id' => $chatDetailId])->delete();
            $timeNow = time();
            $data = [];
            if ($status > 0 && $questionType > 0)
            {
                if ($feedbackReasonArray)
                {
                    foreach ($feedbackReasonArray as $fid)
                    {
                        $group = sprintf("%d%d%d%02d", $qid, $questionType, $status, $fid);
                        $data[] = [
                            'group' => (int)$group,
                            'uid' => $uid,
                            'chat_detail_id' => $chatDetailId,
                            'status' => $status,
                            'feedback_id' => $fid,
                            'pid' => $pid,
                            'create_time' => $timeNow
                        ];
                    }
                } else
                {
                    $group = sprintf("%d%d%d%02d", $qid, $questionType, $status, 0);
                    $data[] = [
                        'group' => (int)$group,
                        'uid' => $uid,
                        'chat_detail_id' => $chatDetailId,
                        'status' => $status,
                        'feedback_id' => 0,
                        'pid' => $pid,
                        'create_time' => $timeNow
                    ];
                }
            }
            if ($data)
            {
                $this->saveAll($data);
            }
            return true;
        }
        catch (\Exception $e)
        {
            Log::write('HzChatRecordDetailsZanData model => setZan()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }
    }
}
